﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using APC.Common;


namespace APC
{ 

    public partial class frmPickList : Telerik.WinControls.UI.RadForm
    {
        #region Events

        public delegate void Item_Selected(object o, EventArgs e);
        public event Item_Selected OnItemSelected;

        #endregion

        #region Enum

        public enum ObjectType { TypeLocation, TypeRecipe, TypeRawMaterial, TypeStorageLocation }

        /// <summary>
        /// SingleSelect = closes pick list after 1 selection, MultipleSelect = keeps pick list open while selecting
        /// </summary>
        public enum SelectType { TypeSingleSelect, TypeMultipleSelect }

        #endregion

        #region Declarations

        private IconLibrary ImageList;
        private int RawMaterialId;

        private SelectType _SelectType;
        private ObjectType _ObjectType;

        #endregion

        #region Constructors

        public frmPickList(int Id, SelectType SelectType, ObjectType ObjectType)
        {
            InitializeComponent();

            _SelectType = SelectType;
            _ObjectType = ObjectType;

            RawMaterialId = Id;

            switch (_ObjectType)
            {
                case frmPickList.ObjectType.TypeLocation:
                    LoadLocations();
                    break;

                case frmPickList.ObjectType.TypeRawMaterial:
                    LoadRawMaterials();
                    break;

                case frmPickList.ObjectType.TypeRecipe:
                    LoadRecipes();
                    break;

                case frmPickList.ObjectType.TypeStorageLocation:
                    LoadStorageLocations();
                    break;

            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Returns icon based on icon name. E.g."folder"
        /// </summary>
        /// <param name="MenuIcon"></param>
        /// <returns></returns>
        private Image SetIcon(String Icon)
        {
            int i;
            Image img = null;

            i = ImageList.Img16x16.Images.IndexOfKey(Icon);

            if (i > -1)
            {
                img = ImageList.Img16x16.Images[i];
            }
            return img;
        }

        private void LoadStorageLocations()
        {
        
            ImageList = new IconLibrary();
            ListViewDataItem lst;

            List<APC.Common.Location> LocationList;

            Locations location = new Locations(true);

            LocationList = location.LocationList;

            this.Text = "Select Storage";

            lstPickList.Columns[1].HeaderText = "Storage";
            lstPickList.Columns[1].Width = 200;
            lstPickList.Columns[2].HeaderText = "";
            lstPickList.Columns[2].Width = 80;
            lstPickList.Columns[3].HeaderText = "";
            lstPickList.Columns[3].Width = 80;

            lstPickList.Refresh();
     
                foreach (var l in LocationList.Where(x => x.LocationTypeId == 6))
                {
                    lst = new ListViewDataItem();
                    lst.Tag = l;
                    lst.Image = SetIcon("silo");
                    lst.SubItems.Add("");
                    lst.SubItems.Add(l.Name);
                    lst.SubItems.Add("");

                    lstPickList.Items.Add(lst);                
                }
        }

        private void LoadLocations()
        {
            lstPickList.ShowGroups = true;
            lstPickList.EnableCustomGrouping = true;

            ImageList = new IconLibrary();
            ListViewDataItem lst;

            List<APC.Common.Location> LocationList;
            List<APC.Common.LocationsType> LocationTypeList;

            Locations location = new Locations(true);

            LocationList = location.LocationList;
            LocationTypeList = location.LocationTypes;

            this.Text = "Select Location";

            lstPickList.Columns[1].HeaderText = "Location";
            lstPickList.Columns[1].Width = 200;
            lstPickList.Columns[2].HeaderText = "";
            lstPickList.Columns[2].Width = 80;
            lstPickList.Columns[3].HeaderText = "";
            lstPickList.Columns[3].Width = 80;

            lstPickList.Refresh();

            ListViewDataItemGroup lstGrp;

            foreach (var lt in LocationTypeList)
            {

                lstGrp = new ListViewDataItemGroup(lt.Name.Trim());
                lstPickList.Groups.Add(lstGrp);

                foreach (Location l in LocationList.Where(x => x.LocationTypeId == lt.Id))
                {

                    lst = new ListViewDataItem();
                    lst.Tag = l;
                    lst.Image = SetIcon("silo");
                    lst.SubItems.Add("");
                    lst.SubItems.Add(l.Name);
                    lst.SubItems.Add("");

                    lstPickList.Items.Add(lst);
                    lst.Group = lstGrp;

                }

            }
        }

        private void LoadRecipes()
        {
            ImageList = new IconLibrary();
            ListViewDataItem lst;

            List<APC.Common.Recipe> RecipeList;

            Recipe recipe = new Recipe();

            RecipeList = recipe.RecipeList;

            this.Text = "Select Recipe";

            lstPickList.Columns[1].HeaderText = "Number";
            lstPickList.Columns[2].HeaderText = "Recipe";
            lstPickList.Columns[2].Width = 200;
            lstPickList.Columns.Remove("Column 2");
            

            lstPickList.Refresh();

            foreach (Common.Recipe r in RecipeList)
            {

                lst = new ListViewDataItem();
                lst.Tag = r;
                lst.Image = SetIcon("recipe");
                lst.SubItems.Add("");
                lst.SubItems.Add(r.RecipeNumber);
                lst.SubItems.Add(r.RecipeName);
     
                lstPickList.Items.Add(lst);

            }
        }

        private void LoadRawMaterials()
        {

            lstPickList.ShowGroups = true;
            lstPickList.EnableCustomGrouping = true;
            //lstPickList.EnableGrouping = true;

            ImageList = new IconLibrary();
            ListViewDataItem lst;

            List<RawMaterial> RawMaterialList;
            List<RawMaterialGroup> RawMaterialGroup;
            List<NegativeRawMaterial> NegativeList;

            RawMaterials rm = new RawMaterials(true);

            RawMaterialList = rm.RawMaterialList;
            RawMaterialGroup = rm.RawMaterialGroups;
            NegativeList = rm.RawMaterialNegativeList;

            ListViewDataItemGroup lstGrp;

            foreach (var grp in RawMaterialGroup)
            {
                lstGrp = new ListViewDataItemGroup(grp.Name.Trim());
                lstPickList.Groups.Add(lstGrp);
           
                foreach (RawMaterial r in RawMaterialList.Where(x => x.RawMaterialGroupId == grp.Id))
                {
                    lst = new ListViewDataItem();
                    lst.Tag = r;
                    lst.Image = SetIcon("ingridient");
                    lst.SubItems.Add("");
                    lst.SubItems.Add(grp.Name);
                    lst.SubItems.Add(r.MaterialNumber);
                    lst.SubItems.Add(r.Name);

                    lstPickList.Items.Add(lst);
                    lst.Group = lstGrp;
                }
            }

            int i = lstPickList.Groups.Count;
            int y = lstPickList.Items.Count;

        }

        #endregion

        #region Event Handling

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lstPickList_ItemMouseDoubleClick(object sender, ListViewItemEventArgs e)
        {
            if (lstPickList.SelectedItems.Count > 0)
            {
                ListViewDataItem lst = lstPickList.SelectedItems[0];

                OnItemSelected(lst.Tag, e);

                if (_SelectType == SelectType.TypeSingleSelect)
                {
                    this.Close();
                }
            }
        }

        private void mnuAddSelectedRawMaterial_Click(object sender, EventArgs e)
        {
            if (lstPickList.SelectedItems.Count > 0)
            {
                ListViewDataItem lst = lstPickList.SelectedItems[0];
                
                OnItemSelected(lst.Tag, e);

                if (_SelectType == SelectType.TypeSingleSelect)
                {
                    this.Close();
                }
            }
        }

        #endregion

    }
}
